VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "LOCXLReport"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Implements ILOCReport

Private wbOut As Worksheet
Private row As Long
Private theModule As VBModule
Private simpleProcs As Long
Private report As Variant

Const kNoShowLevel = 5

Private Sub ILOCReport_printReport(modulesForReporting As VBModules, outputPath As String)
    printReport modulesForReporting, outputPath
End Sub

Private Sub printReport(modulesForReporting As VBModules, outputSheetName As String)

Dim idx As Long

    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False

    Set wbOut = ThisWorkbook.Worksheets(outputSheetName)
    
    With wbOut.Cells
        .Clear
        .ClearNotes
    End With
    
    row = 0
    
    ReDim report(1 To 2000, 1 To 15) As Variant
    
    WriteTotals modulesForReporting
    
    For idx = 1 To modulesForReporting.Count
        Set theModule = modulesForReporting.Module(idx)
        simpleProcs = 0
        PrintModuleHeader
        PrintListRows
        PrintModuleFooter
    Next

    wbOut.Activate
    
    wbOut.Cells(1, 1).Resize(2000, 15) = report
    
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic

End Sub

Private Sub WriteTotals(mods As VBModules)

Dim idx As Long
Dim totLines As Long
Dim totProcs  As Long
Dim totFileSize As Long
Dim totGlobs As Long

    For idx = 1 To mods.Count
        Set theModule = mods.Module(idx)
        With theModule
            totLines = totLines + .LineCount
            totProcs = totProcs + .ProcCount
            totFileSize = totFileSize + .FileSize
            totGlobs = totGlobs + .GlobalVarCount
        End With
    Next
    
    PutRow 1, Array("Total Lines", totLines)
    PutRow 1, Array("Total Procs", totProcs)
    PutRow 1, Array("Total Filesize", totFileSize)
    PutRow 1, Array("Total Globals", totGlobs)
    
    PutBlankRow

End Sub

Private Sub PrintModuleHeader()

    PutBlankRow
    PutRow 1, Array(theModule.Name & " (" & theModule.ModuleTypeString & ")", "", "", "Globals", "Members", "Procs")
    SetRowStyles 1, Array("1:ModuleName", "2:", "3:StatHead1")
    SetRowStyles 1, Array("11:LineAbove")
    
    PutRow 4, Array(theModule.GlobalVarCount, theModule.MemberVarCount, theModule.ProcCount)

End Sub

Private Sub SetRowStyles(ByVal col As Long, arrStyles As Variant)

Dim styleInfo As Variant
Dim arrInfo As Variant
Dim cols As Long
Dim styleName As String
Dim target As range

    For Each styleInfo In arrStyles
        arrInfo = Split(styleInfo, ":")
        cols = CLng(arrInfo(0))
        styleName = CStr(arrInfo(1))
        If styleName <> "" Then
            wbOut.Cells(row, col).Resize(1, cols).Style = styleName
        End If
        col = col + cols
    Next

End Sub

Private Sub PrintListRows()

Dim proc As VBProc

    If theModule.ProcCount = 0 Then
        Exit Sub
    End If

    If theModule.HasProcsMoreComplexThan(kNoShowLevel) Then
    
        PutRow 1, Array("Procedure", "Lines", "Max nest", "Comments", "Declares", "Ifs", "Loops", "Long lines", "Go Tos", "OCV", "X")
        SetRowStyles 2, Array("10:StatHead2")
    
        For Each proc In theModule.Procedures
            printRow proc
        Next
    
    End If

End Sub

Private Sub PrintModuleFooter()

    If simpleProcs > 0 Then
        PutRow 3, Array(simpleProcs & " proc(s) not reported (complexity <=" & kNoShowLevel & ")")
    End If

End Sub

Private Sub printRow(procToPrint As VBProc)

    With procToPrint
        If .Complexity <= kNoShowLevel Then
            simpleProcs = simpleProcs + 1
        Else
            PutRow 1, Array(.Name, .CounterValue(ecLines), .CounterValue(ecHighestNestLevel) _
                , .CounterValue(ecComments), .CounterValue(ecDeclares), .Counters.CombinedIfs, .CounterValue(ecLoops) _
                , .CounterValue(ecLongLines), .CounterValue(ecGotos), .CounterValue(ecOneCharVars) _
                , .Counters.Complexity _
                )
        End If
    End With

End Sub

Private Sub PutRow(startCol As Long, arr As Variant)

Dim col As Long
    
    row = row + 1

    For col = LBound(arr) To UBound(arr)
        report(row, startCol + col - LBound(arr)) = arr(col)
    Next
    
End Sub

Private Sub PutBlankRow()
    row = row + 1
End Sub

